package com.hs.pre.model;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;

import com.hs.HsBase;
import com.hs.core.db.DBOperator;

public class JobEntry extends BaseEntry{
   
	private long jobId;
	private String jobType;
	private String jobDesc;
	private Date jobStart;
	private Date jobEnd;
	private String jobStatus = "1";
	private String exeClass;
	private String relationId;
	private String jobExeTime;
	public static String JOB_STATUS_ONE = "1";//未执行
	public static String JOB_STATUS_TWO = "2";//执行中
	public static String JOB_STATUS_THREE = "3";//已完成
	public static String JOB_TYPE_ONE = "1";//同时
	public static String JOB_TYPE_TWO = "2";//定时
	public static String JOB_TYPE_THREE = "3";//批量
	
	
	public JobEntry(Map data){
		if(null != data){
			if(data.get("JOB_ID") != null)
			  this.jobId = new Long(data.get("JOB_ID")+"").longValue();
			this.jobType = data.get("JOB_TYPE")+"";
			this.jobDesc = data.get("JOB_DESC")+"";
			this.jobStart = (Date) ((Timestamp)data.get("JOB_START")==null?null:data.get("JOB_START"));
			this.jobEnd = (Date) ((Timestamp)data.get("JOB_END")==null?null:data.get("JOB_START"));
			this.jobStatus = data.get("JOB_STATUS")+"";
			this.exeClass = data.get("JOB_EXE_CLASS")+""; 
			this.relationId = data.get("RELATION_ID")+""; 
			this.jobExeTime = data.get("JOB_EXE_TIME")+""; 
		}
	}
	public JobEntry(){
		
	}
	public long getJobId() {
		return jobId;
	}
	public void setJobId(long jobId) {
		this.jobId = jobId;
	}
	public String getJobType() {
		if(null != jobType){
			if("1".equalsIgnoreCase(jobType)){
				return "立即执行";
			}
			if("2".equalsIgnoreCase(jobType)){
				return "定时执行";
			}
			if("3".equalsIgnoreCase(jobType)){
				return "批量执行";
			}
		}
		return jobType;
	}
	public String getJobTypeCode() {
		return jobType;
	}
	public void setJobType(String jobType) {
		this.jobType = jobType;
	}
	public String getJobDesc() {
		return jobDesc;
	}
	public void setJobDesc(String jobDesc) {
		this.jobDesc = jobDesc;
	}
	public Date getJobStart() {
		return jobStart;
	}
	public void setJobStart(Date jobStart) {
		this.jobStart = jobStart;
	}
	public Date getJobEnd() {
		return jobEnd;
	}
	public void setJobEnd(Date jobEnd) {
		this.jobEnd = jobEnd;
	}
	public String getJobStatus() {
		if(null != jobType){
			if(JOB_STATUS_ONE.equalsIgnoreCase(jobStatus)){
				return "未执行";
			}
			if(JOB_STATUS_TWO.equalsIgnoreCase(jobStatus)){
				return "执行中";
			}
			if(JOB_STATUS_THREE.equalsIgnoreCase(jobStatus)){
				return "已完成";
			}
		}
		return jobStatus;
	}
	public void setJobStatus(String jobStatus) {
		this.jobStatus = jobStatus;
	}
	public String getExeClass() { 
		return exeClass;
	}
	public void setExeClass(String exeClass) {
		this.exeClass = exeClass;
	}
	public String getRelationId() {
		return relationId;
	}
	public void setRelationId(String relationId) {
		this.relationId = relationId;
	}
	public int updateJob(){ 
		//if(JOB_STATUS_THREE.equals(this.jobStatus)){
			this.jobEnd = new Timestamp(System.currentTimeMillis());
		//}
		if(JOB_STATUS_ONE.equals(this.jobStatus)){
			this.jobStatus = JOB_STATUS_THREE;
		}else if(JOB_STATUS_TWO.equals(this.jobStatus)){
			this.jobStatus = JOB_STATUS_THREE;
		} 
		DBOperator dbo = this.getDBOperator();
		String sql = "";
		Object[] objs = null; 
		if(null != this.jobEnd){
			sql = HsBase.getAnsiSqlMap().get("pre_job.update")+"";
			objs = new Object[]{this.jobType,this.jobEnd,this.jobStatus,this.jobExeTime,this.getJobId()};
		}else{
			sql = HsBase.getAnsiSqlMap().get("pre_job.update_byid")+"";
			objs = new Object[]{this.jobType,this.jobStatus,this.getJobId()};
		}
		System.out.println("sql:" + sql);
		return dbo.update(sql, objs);
		 
		
	}
	public int insertJob(){
		DBOperator dbo = this.getDBOperator();
		String sql = HsBase.getAnsiSqlMap().get("pre_job.insert")+"";
		return dbo.update(sql, new Object[]{this.jobDesc,this.jobType,new Timestamp(System.currentTimeMillis()),JOB_STATUS_ONE,this.getExeClass(),this.getRelationId()});
	}
	public String getJobExeTime() {
		return jobExeTime;
	}
	public void setJobExeTime(String jobExeTime) {
		this.jobExeTime = jobExeTime;
	}
	 
}
